package com.example.springsecurity.serivce;

import com.example.springsecurity.entity.SysRole;
import com.example.springsecurity.entity.User;
import com.example.springsecurity.security.AuthenticationThreadLocal;
import com.example.springsecurity.security.dto.LoginUserDto;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Set;

/**
 * @author wuancheng
 * @date 2024/6/20 10:25
 * @description
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.根据用户名校验用户的有效性
        User user = new User();

        // 2.校验登录密码，并做登录失败次数限制等
        Authentication authentication = AuthenticationThreadLocal.getContext();
        String userName = authentication.getName();
        String password = authentication.getCredentials().toString();

        // 3.登录成功填充用户菜单数据权限返回
        Set<String> permissions = new HashSet<>();
        permissions.add("user:info:my");
        Set<SysRole> roles = new HashSet<>();
        return new LoginUserDto(1L ,userName, password, permissions, roles);
    }

}
